import 'package:flutter/material.dart';
import 'package:ichinesereader/controller/schooladmin.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/pages/schooladmin/adminProfileEditor.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerSchoolAdmin.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/sidebars/adminSidebar.dart';
import '../../themes/appTheme.dart';

class SchoolAdminProfilePage extends StatefulWidget {
  static const String routeName = '/schooladmin/profile';

  @override
  State<SchoolAdminProfilePage> createState() => _SchoolAdminProfilePageState();
}

class _SchoolAdminProfilePageState extends State<SchoolAdminProfilePage> {
  final SchoolAdminController _schoolAdminController = Get.find();
  final box = Hive.box('appconfig');

  @override
  void initState() {
    bool isSchoolAdmin = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_SchoolAdmin;
    if (isSchoolAdmin) {
      Future.delayed(Duration(milliseconds: 100), (() {
        _schoolAdminController.fetchSchoolAdminProfile();
      }));
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build SchoolAdminProfilePage');

    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.schoolAdminPageItems
        .indexWhere((page) => page.route == Get.currentRoute);
    final PageStyles _pageStyles = PageStyles();

    return Scaffold(
        appBar: isSmallScreen ? CustomAppBar(title: 'Profile'.tr) : null,
        drawer: isSmallScreen ? SchoolAdminAppDrawer() : null,
        body: Container(
          decoration: _pageStyles.pageBoxDecoration(),
          child: Row(
            children: [
              Visibility(
                  visible: !isSmallScreen,
                  child: AdminSidebarX(
                      selectedIndex: _selectedSidebarItem,
                      isBigScreen: isBigScreen)),
              Expanded(
                child: Container(
                  margin: EdgeInsets.fromLTRB(0.0, 33.0, 12.0, 33.0),
                  padding: EdgeInsets.all(12.0),
                  decoration: BoxDecoration(
                    color: Get.isDarkMode
                        ? AppTheme.colors.darkTheme2
                        : Colors.white.withOpacity(0.3),
                    borderRadius: BorderRadius.circular(3.0),
                  ),
                  child: Container(
                    decoration: BoxDecoration(
                        color: Get.isDarkMode
                            ? AppTheme.colors.darkTheme2
                            : Colors.white),
                    child: _schoolAdminController.obx(
                      (data) {
                        if (_schoolAdminController
                                .schoolAdminProfile.value.userName !=
                            null) {
                          return AdminProfileEditor();
                        } else {
                          return Text('');
                        }
                      },
                    ),
                  ),
                ),
              ),
            ],
          ),
        ));
  }
}
